import { Sprite } from 'cc';
import { Label } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { loadCommonPreFab, showErrorTip, showSelectList, showTip } from '../../tools/PopuUp';
import { App } from '../../App';
import { MultiMsgMgr } from './MultiMsgMgr';
import { find } from 'cc';
import AbstractComponent from '../../base/AbstractComponent';
import { EditBox } from 'cc';
import { PD } from '../../player/PlayerData';
import { Button } from 'cc';
import { MultiCommon } from './MultiCommon';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

enum SelectType {
    GAME_TYPE       = "select_game_type",
    PLAYER_NUMBER   = "select_player_number",
    PLAYER_NUMBER2  = "select_player_number2",
    WATCH           = "select_watch",
}

@ccclass('RoomCreateLayer')
export class RoomCreateLayer extends AbstractComponent {

    @property(Label)
    label_type : Label = null; // 游戏类型

    @property(Label) 
    label_number : Label = null; // 人数
    
    @property(EditBox)
    edit_password : EditBox = null; // 密码

    @property(Label)
    label_watch : Label = null; // 是否可观战

    @property(EditBox)
    edit_free : EditBox = null; // 入场金额

    @property(Button)
    btn_create : Button = null; // 创建按钮

    @property(Button)
    btn_modify : Button = null; // 修改按钮

    roomType : number = 0; // 0 普通场 1 专业场
    canWatch : boolean = true; // 是否可观战
    playAmount : number = 0; // 底分
    maxPlayer : number = 3; // 最大人数
    gameType : number = 0; // 游戏类型 0 多人对战 1 四轮台球

    mode = 0; // 0 创建房间 1 修改房间

    // 选择列表
    SELECT_LIST = {

        // 游戏类型
        "select_game_type" : [
            App.language.getLanguageLab(116),
            App.language.getLanguageLab(117),
        ], 
    
        // 人数，多人对战
        "select_player_number" : [
            // 2,3,4,5,6,7,8,9,10,11,12,13,14,15
            3,4,5
        ],
    
        // 人数, 四轮台球，只能是2个人
        "select_player_number2" : [2],
    
        // 是否可观战
        "select_watch" : [
            App.language.getLanguageLab(2001),
            App.language.getLanguageLab(2002),
        ],
    
    }

    start() {
    }

    // 初始化列表默认项
    initData(roomType) {
        this.roomType = roomType

        this.btn_create.node.active = true
        this.btn_modify.node.active = false

        // 设置默认项
        this.label_type.string = App.language.getLanguageLab(116)
        this.label_watch.string = App.language.getLanguageLab(2001)
    }

    // 设置可修改界面, 只有房主才可以修改
    initModifyUi(data, roomType, isMaster) {

        UtilTool.printLog("test initModifyUi", data, data.password)

        this.mode = 1 // 标记为修改房间

        this.roomType = roomType
        
        this.label_type.string = data.gameType == 0 ? App.language.getLanguageLab(116) : App.language.getLanguageLab(117) // 房间类型
        this.label_number.string = data.maxPlayer.toString() // 人数
        this.label_watch.string = data.canWatch ? App.language.getLanguageLab(2001) : App.language.getLanguageLab(2002) // 是否可观战
        this.edit_password.string = "******" // 密码

        this.edit_free.string = data.playAmount.toString() // 入场金额

        this.canWatch = data.canWatch // 是否可观战
        this.maxPlayer = data.maxPlayer // 最大人数
        this.gameType = data.gameType // 房间类型
        this.playAmount = data.playAmount // 入场金额


        // 只可以修改观战功能
        this.label_type.node.parent.getChildByName('btn_more').active = false
        this.label_type.node.parent.getComponent(Button).enabled = false
        UtilTool.imgToGray(this.label_type.node.parent) // 游戏类型置灰

        this.label_number.node.parent.getChildByName('btn_more').active = false
        this.label_number.node.parent.getComponent(Button).enabled = false
        UtilTool.imgToGray(this.label_number.node.parent) // 人数置灰

        this.label_watch.node.parent.getChildByName('btn_more').active = isMaster
        this.label_watch.node.parent.getComponent(Button).enabled = isMaster
        this.edit_free.enabled = false
        this.edit_password.enabled = false
        this.btn_create.node.active = false
        this.btn_modify.node.active = isMaster

        UtilTool.imgToGray(this.edit_password.node.parent) // 密码置灰
        UtilTool.imgToGray(this.edit_free.node.parent) // 金币编辑框置灰
        UtilTool.imgToGray(this.edit_free.node.parent.getChildByName("icon_coin")) // 金币图标置灰

    }

    onBtnClose() {
        this.clickClose()
    }
    
    async onTouchFree() {
        UtilTool.printLog("onTouchFree");
    }

    // 创建房间
    onBtnCreate() {
        
        this.playClickAudio()

        let playAmountStr = this.edit_free.string
        let passwordStr = this.edit_password.string
        let playAmount = parseInt(playAmountStr)

        // 请输入密码
        // if(passwordStr == "") {
        //     showTip(App.language.getLanguageLab(2003))
        //     return
        // }

        // 请输入入场金额
        if(playAmountStr == "") {
            showTip(App.language.getLanguageLab(2004))
            return
        }

        // 入场金额不能为0
        if(playAmount <= 0) {
            showTip(App.language.getLanguageLab(2005))
            return
        }

        // 当前金额不足以支付入场金额
        if(playAmount > PD.user.coin) {
            showTip(App.language.getLanguageLab(2006))
            return
        }

        // 入场金额最小为xx金币
        if (playAmount < PD.user.matchMinGold) {
            showTip(UtilTool.formatStr(App.language.getLanguageLab(180),PD.user.matchMinGold))
            return
        }
        
        // 提示四轮台球暂未开放
        // if(this.gameType == 1) {
        //     let text = UtilTool.formatStr("%s%s", App.language.getLanguageLab(117), App.language.getLanguageLab(86))
        //     showTip(text)
        //     return
        // }

        UtilTool.printLog("test create room ", this.roomType, this.gameType, passwordStr, this.canWatch, this.maxPlayer, playAmount)

        MultiMsgMgr.getInstance().sendCreateRoom(this.roomType, this.gameType, passwordStr, this.canWatch, this.maxPlayer, playAmount)
        .then((data) => {
            UtilTool.printLog("test create room back ", data);
            
            if(data.result == 0) {
                // App.Event.emit(EventConst.ROOM_LIST_REFRESH)
                // this.node.destroy()

                // 创建成功后就进入到加入房间
                MultiCommon.enterRoom(this.roomType, data.roomId, passwordStr, (data) => {
                    showTip(App.language.getLanguageLab(2029))
                    this.node.destroy()
                })
            } else {
                showErrorTip(data.result)
            }
        })

    }

    // 修改房间
    onBtnModify() {
        UtilTool.printLog("test onBtnModify");

        this.playClickAudio()

        MultiMsgMgr.getInstance().sendModifyRoom(this.canWatch)
        .then((data) => {
            UtilTool.printLog("test modify room back ", data);

            if(data.result == 0) {
                showTip(App.language.getLanguageLab(2025)) // 提示修改成功
                this.close()
            } else {
                showErrorTip(data.result)
            }

        })
    }

    // 点击更多
    onBtnMore(event, id) {

        this.playClickAudio()

        id = parseInt(id)   

        switch(id) {
            case 0: // 游戏类型
                this.showSelectListNode(SelectType.GAME_TYPE)
                break;

            case 1: // 人数
                if(this.gameType == 0) {
                    this.showSelectListNode(SelectType.PLAYER_NUMBER) // 多人对战的人数列表
                } else {
                    this.showSelectListNode(SelectType.PLAYER_NUMBER2) // 四轮台球的人数列表
                }
                break;

            case 2: // 是否可观战
                this.showSelectListNode(SelectType.WATCH)
                break;
                
        }

    }

    // 显示选择列表
    showSelectListNode(selectType : SelectType) {

        let textList = this.SELECT_LIST[selectType]

        // 返回的选择项
        let callBack = (index)=>{

            UtilTool.printLog("test select index ", index);

            switch(selectType) {
                case SelectType.GAME_TYPE: // 游戏类型
                    this.label_type.string = textList[index].toString()
                    
                    // 如果更换到不同类型时， 四轮台球时，人数默认为2, 多人对战时，默认为3
                    if(this.gameType != index) {
                        if(index == 0) {
                            let playCount = this.SELECT_LIST[SelectType.PLAYER_NUMBER][0]
                            this.label_number.string = playCount.toString()
                            this.maxPlayer = playCount
                        } else {
                            this.label_number.string = "2"
                            this.maxPlayer = 2
                        }
                    }

                    this.gameType = index
                    break;

                case SelectType.PLAYER_NUMBER: // 人数
                    this.label_number.string = textList[index].toString()
                    this.maxPlayer = Number(textList[index])
                    break;

                case SelectType.WATCH: // 是否可观战
                    this.label_watch.string = textList[index].toString()
                    this.canWatch = index == 0
                    break;

            }
        }
        
        showSelectList(this.node, textList, callBack)
        
    }

}

